// src/components/ChangeAvatar.tsx
import React, { useState } from 'react'
import { View, Image, Button } from '@tarojs/components'
import Taro from '@tarojs/taro'
import './ChangeAvatar.scss'

interface ChangeAvatarProps {
  defaultAvatar?: string
  onChange?: (newAvatar: string) => void
}

const ChangeAvatar: React.FC<ChangeAvatarProps> = ({
                                                     defaultAvatar = 'https://taro-ui.jd.com/static/images/avatar.png',
                                                     onChange
                                                   }) => {
  const [avatar, setAvatar] = useState(defaultAvatar)

  const handleChangeAvatar = () => {
    Taro.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        const filePath = res.tempFilePaths[0]
        setAvatar(filePath)
        onChange?.(filePath)

        // 如果需要上传，可以在此调用上传逻辑：
        // Taro.uploadFile({ url, filePath, name, ... })
      },
      fail: err => {
        Taro.showToast({ title: '选择头像失败', icon: 'none' })
        console.error('选择头像失败', err)
      }
    })
  }

  return (
    <View className="change-avatar">
      <View className="avatar-wrapper" onClick={handleChangeAvatar}>
        <Image src={avatar} className="avatar-img" />
      </View>
      {/*<Button className="change-btn" onClick={handleChangeAvatar}>更换头像</Button>*/}
    </View>
  )
}

export default ChangeAvatar
